//
//  AppDelegate.swift
//  swiftweibo6
//
//  Created by ysz on 15/11/22.
//  Copyright © 2015年 user. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        window=UIWindow(frame: UIScreen.mainScreen().bounds)
//        let tabbar = YSZMainController()
        
//        window?.rootViewController=tabbar
//        window?.rootViewController=YSZWelcomeViewController()
//        
//        window?.rootViewController=YSZNewFeatureCollectionViewController()
        print(NSHomeDirectory())
        window?.rootViewController=defaultController()
        window?.makeKeyAndVisible()
        
        setupAppearace()
        
        return true
    }
    //判断进入那个控制器
    private func defaultController() -> UIViewController
    {
        //用户是否登录，没有则显示访客页面
        if !YSZUserAccount.userLogin
        {
            return YSZMainController()
        }
        //是否时新版本 是返回新特性 不是返回欢迎界面
        return isNewFeature() ? YSZNewFeatureCollectionViewController() : YSZWelcomeViewController()
    }
    
    //其他界面切换需通过AppDelegate
    private func switchViewController(isMain: Bool)
    {
        window?.rootViewController = isMain ? YSZMainController() : YSZWelcomeViewController()
    }
    // 类方法切换控制器 方便调用
    class func switchRootViewController(isMain: Bool)
    {
        (UIApplication.sharedApplication().delegate as? AppDelegate)?.switchViewController(isMain)
    }
    
    //判断是否是新版本
    private func isNewFeature() -> Bool
    {
        //获取当前版本
        let currentVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        //获取上一次版本
        let sandboxVersionKey = "sandboxVersionKey"
        
        let sandboxVersion = NSUserDefaults.standardUserDefaults().doubleForKey(sandboxVersionKey)
        //保存当前版本
        NSUserDefaults.standardUserDefaults().setDouble(currentVersion, forKey: sandboxVersionKey)
        NSUserDefaults.standardUserDefaults().synchronize()
        print("currentVersion:\(currentVersion)")
        print("sandboxVersion:\(sandboxVersion)")
        //比较
        return currentVersion != sandboxVersion
      
    }
    
    //设置导航栏皮肤
    private func setupAppearace()
    {
        UINavigationBar.appearance().tintColor=UIColor.orangeColor()
    }

}

